import { GSprite } from "../../../../../../../com/mui/controls/GSprite";
import { ObjCreate } from "../../../../common/ObjCreate";
import { ComposeJewelNormalSelecter } from "./ComposeJewelNormalSelecter";
import { GModuleEvent } from "../../../../../mvc/GModuleEvent";
import { ComposeJewelSpecialSelecter } from "./ComposeJewelSpecialSelecter";
import { NormalItemCellRenderer } from "../../../../common/item/NormalItemCellRenderer";
import { DataProvider } from "../../../../../../../fl/data/DataProvider";
import { Language } from "../../../../../../../extend/language/Language";
import { GlobalSkin } from "../../../../../../component/skin/GlobalSkin";
import { HTMLUtil } from "../../../../../../../com/gengine/utils/HTMLUtil";
import { ImagesConst } from "../../../../../resource/ImagesConst";
import { GlobalStyle } from "../../../../../../common/global/GlobalStyle";
import { MountWashMaterialCellRenderer } from "../../../../mounts/cell/MountWashMaterialCellRenderer";
import { EquipmentModuleConst } from "../../../data/EquipmentModuleConst";
import { ComposeJewelChangeBtn } from "./ComposeJewelChangeBtn";
import { ComposeJewelMode } from "./ComposeJewelMode";
import { MaterialItemData } from "../../../MaterialItemData";
import { ItemData } from "../../../../../resource/info/ItemData";
import { ShopConfig } from "../../../../../resource/ShopConfig";
import { ShopRule } from "../../../../../rules/ShopRule";
import { Cache } from "../../../../../cache/Cache";
import { DataEvent } from "../../../../../events/DataEvent";
import { ShopItemInfo } from "../../../../../resource/info/ShopItemInfo";
import { ColorConfig } from "../../../../../resource/ColorConfig";
import { GameDefConfig } from "../../../../../resource/GameDefConfig";
import { Alert } from "../../../../../../../com/mui/controls/Alert";
import { Dispatcher } from "../../../../../../mvc/core/Dispatcher";
import { EventName } from "../../../../../mvc/EventName";
import { MsgManager } from "../../../../../manager/MsgManager";
import { ItemsUtil } from "../../../../shortcuts/item/ItemsUtil";
import { TodayNotTipUtil } from "../../../../../../common/TodayNotTipUtil";
import { TodayNoTipsConst } from "../../../../../../component/gconst/TodayNoTipsConst";
import { ECategory } from "../../../../../../../Message/Public/ECategory";
import { EProp } from "../../../../../../../Message/Public/EProp";
import { JewelUtil } from "../../../../common/util/JewelUtil";
import { EquipAdvanceConfig } from "../../../../../resource/EquipAdvanceConfig";
import { GTileList } from "../../../../../../../com/mui/controls/GTileList";
import { GButton } from "../../../../../../../com/mui/controls/GButton";
import { GLabel } from "../../../../../../../com/mui/controls/GLabel";
import { GCheckBox } from "../../../../../../../com/mui/controls/GCheckBox";
import { GTextFiled } from "../../../../../../../com/mui/controls/GTextFiled";
type int = number;
//class ComposeJewelOneKey
    
    export  class ComposeJewelOneKey extends GSprite
    {
        constructor()
        {
            
            this.initUI();
            return;
        }

        /* internal  */initUI(): void
        {
            ObjCreate.createFlowerBg(19, 60, 415, 315, this);
            ObjCreate.createBg(30, 140, 393, 129, this);
            ObjCreate.createLoadedBitmap("composeJewel_xzxyhcdbslx.png", 124, 71, this);
            ObjCreate.createLoadedBitmap("composeJewel_tip.png", 99, 278, this);
            ObjCreate.createLoadedBitmap("composeJewel_optBtnBg.png", 124, 300, this);
            this._normalSelecter = new ComposeJewelNormalSelecter();
            this._normalSelecter.x = 86;
            this._normalSelecter.y = 110;
            this._normalSelecter.addEventListener(GModuleEvent.ComposeJewelSelecterChange, this.onJewelSelecterChangeHandler);
            this.addChild(this._normalSelecter);
            this._specialSelecter = new ComposeJewelSpecialSelecter();
            this._specialSelecter.x = 249;
            this._specialSelecter.y = 110;
            this._specialSelecter.addEventListener(GModuleEvent.ComposeJewelSelecterChange, this.onJewelSelecterChangeHandler);
            this.addChild(this._specialSelecter);
            this._jewelList = ObjCreate.createTileList(38, 144, 377, 120, this);
            this._jewelList.columnWidth = 40;
            this._jewelList.rowHeight = 40;
            this._jewelList.setStyle("cellRenderer", NormalItemCellRenderer);
            this._jewelDp = new DataProvider();
            this._jewelList.dataProvider = this._jewelDp;
            this._optBtn = ObjCreate.createGButton(Language.getString(61096), 189, 311, 75, 28, this, GlobalSkin.ButtonRed);
            this._optBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onOptBtnClickHandler);
            this._useBindMaterial = ObjCreate.createCheckBox(Language.getString(60323), 26, 345, 150, 28, this);
            this._useBindMaterial.toolTipData = HTMLUtil.addColor(Language.getString(61083), "#FFFFFF");
            this._useBindMaterial.selected = true;
            this._useBindMaterial.addEventListener(flash.events.Event.CHANGE, this.useBindChangeHandler);
            ObjCreate.createLabel(Language.getString(60322), 310, 348, 100, 20, flash.text.TextFormatAlign.LEFT, this, true, 15859633);
            ObjCreate.createBg(344, 349, 75, 18, this, ImagesConst.DisabledBg);
            ObjCreate.createBitmap(ImagesConst.Tongqian_small, 397, 351, this);
            this._costLabel = ObjCreate.createLabel("0", 344, 349, 50, 20, flash.text.TextFormatAlign.LEFT, this, false, 15859633);
            ObjCreate.createBg(19, 378, 415, 80, this);
            this._descText = ObjCreate.createTextField("", 24, 380, 405, 90, this, GlobalStyle.textFormat1);
            this._descText.wordWrap = true;
            this._descText.multiline = true;
            var loc1=Language.getString(61098) + "\n" + Language.getString(61099) + "\n" + Language.getString(61100) + "\n" + Language.getString(61101);
            this._descText.htmlText = "<textformat leading=\'3\' leftMargin=\'19\' indent=\'-19\'>" + loc1 + "</textformat>";
            ObjCreate.createBg(437, 60, 172, 398, this);
            ObjCreate.createBg(437, 62, 139, 24, this, ImagesConst.TileBg);
            ObjCreate.createLoadedBitmap("composeJewel_hccl.png", 460, 65, this);
            this._autoBuy = ObjCreate.createCheckBox(Language.getString(60325), 442, 92, 100, 20, this, "GRadioButton");
            this._buyDirect = ObjCreate.createCheckBox(Language.getString(60326), 442, 112, 100, 20, this, "GRadioButton");
            this._buyDirect.selected = true;
            ObjCreate.createBg(442, 134, 164, 2, this, ImagesConst.SplitLine);
            this._materialList = ObjCreate.createTileList(442, 139, 164, 329, this);
            this._materialList.rowHeight = 68;
            this._materialList.columnWidth = 164;
            this._materialList.setStyle("cellRenderer", MountWashMaterialCellRenderer);
            this._materialList.addEventListener(EquipmentModuleConst.Event_DoBuyEvent, this.buyMaterialHandler);
            this.addChild(this._materialList);
            var loc2=[];
            loc2.push(this.getMaterial(14009200));
            loc2.push(this.getMaterial(14009300));
            loc2.push(this.getMaterial(14009400));
            this._materialDp = new DataProvider(loc2);
            this._materialList.dataProvider = this._materialDp;
            this.updateMaterialNum();
            this._changeModeBtn = new ComposeJewelChangeBtn(ComposeJewelMode.ModeOneKey);
            this._changeModeBtn.x = 20;
            this._changeModeBtn.y = 64;
            this._changeModeBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onChangeModeBtnClick);
            this.addChild(this._changeModeBtn);
            this.setChildIndex(this._normalSelecter, (this.numChildren - 1));
            this.setChildIndex(this._specialSelecter, (this.numChildren - 1));
            return;
        }

        /* internal  */getMaterial(arg1: int): MaterialItemData
        {
            var loc1=new MaterialItemData();
            var loc2=new ItemData(arg1);
            loc1.itemData = loc2;
            var loc3=ShopConfig.instance.getShopItemInfo(loc2.itemCode, ShopRule.ShopMallMaterial);
            loc1.isInShop = true;
            loc1.shopItem = loc3;
            return loc1;
        }

        public updateMaterialNum(): void
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < this._materialDp.length) 
            {
                loc1 = this._materialDp.getItemAt(loc2) as MaterialItemData;
                loc1.itemData.itemAmount = Cache.instance.pack.backPackCache.getAllAoumtOfSameNameItem(loc1.itemData.itemInfo.item.name);
                loc1.hasUnBind = !(Cache.instance.pack.backPackCache.getItemByCode(loc1.code) == null);
                ++loc2;
            }
            this._materialList.dataProvider = this._materialDp;
            return;
        }

        /* internal  */buyMaterialHandler(arg1: DataEvent): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=0;
            var loc4=null;
            var loc5=null;
            arg1.stopImmediatePropagation();
            if (this._buyDirect.selected != false) 
            {
                loc1 = arg1.data.shopItemInfo as ShopItemInfo;
                loc2 = arg1.data.amount as int;
                loc3 = loc1.tshopSell.offer > 0 ? loc1.tshopSell.offer * loc2 : loc1.tshopSell.price * loc2;
                loc4 = ColorConfig.instance.getItemColor(loc1.itemInfo.item.color);
                loc5 = "        " + Language.getStringByParam(61023, HTMLUtil.addColor(loc2 + "", "#FF0000"), HTMLUtil.addColor(loc1.itemInfo.item.name, loc4.color), HTMLUtil.addColor(loc3 + GameDefConfig.instance.getEPrictUnitName(loc1.unit), "#FFEA00"));
                Alert.extendObj = arg1.data;
                Alert.show(loc5, Language.getString(69900), Alert.OK | Alert.CANCEL, null, this.onBuyAlertHanlder);
            }
            else 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallBuyItem, arg1.data));
            }
            return;
        }

        /* internal  */onBuyAlertHanlder(arg1: int, arg2: Object): void
        {
            if (arg1 == Alert.OK) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallBuyItem, arg2));
            }
            return;
        }

        /* internal  */onJewelSelecterChangeHandler(arg1: DataEvent): void
        {
            this.updateJewelDatas();
            return;
        }

        /* internal  */useBindChangeHandler(arg1: Object /* flash.events.Event */): void
        {
            return;
        }

        /* internal  */onChangeModeBtnClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.dispatchEvent(new DataEvent(GModuleEvent.ComposeJewelModeChange));
            return;
        }

        /* internal  */onOptBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc2=null;
            var loc3=null;
            var loc4=null;
            var loc5=0;
            var loc6=null;
            var loc7=false;
            var loc8=false;
            var loc9=null;
            if (Cache.instance.role.moneyCoin < this._cost) 
            {
                MsgManager.showRollTipsMsg(Language.getString(69223));
                return;
            }
            var loc1=new Map<any, any> /* flash.utils.Dictionary */();
            var loc10=0;
            var loc11=this._jewelTypeDic;
            for(loc2 of loc11) 
            {
                loc3 = this.getPropData(loc2.itemLevel);
                if ((loc5 = Math.floor(loc2.amount / 4)) <= 0) 
                {
                    continue;
                }
                if (loc1[loc3.code] == null) 
                {
                    (loc4 = new PropTypeDicData()).prop = loc3.itemData;
                    loc4.amount = loc5;
                    loc4.hasUnBind = loc3.hasUnBind;
                    loc1[loc3.code] = loc4;
                    continue;
                }
                (loc1[loc3.code] as PropTypeDicData).amount = (loc1[loc3.code] as PropTypeDicData).amount + loc5;
            }
            loc6 = "";
            loc7 = false;
            loc8 = false;
            loc10 = 0;
            loc11 = loc1;
            for(loc4 of loc11) 
            {
                loc7 = true;
                if (loc4.prop.itemAmount > 0) 
                {
                    if (this._useBindMaterial.selected || loc4.hasUnBind) 
                    {
                        loc8 = true;
                    }
                }
                loc6 = loc6 + ("[" + ItemsUtil.getItemName(loc4.prop) + " * " + HTMLUtil.addColor(String(loc4.amount), "#FFFF00") + "]");
            }
            if (!loc7) 
            {
                MsgManager.showRollTipsMsg(Language.getString(61109));
                return;
            }
            if (!this._autoBuy.selected && !loc8) 
            {
                MsgManager.showRollTipsMsg(Language.getString(61110));
                return;
            }
            loc9 = Language.getStringByParam(61108, loc6, this._cost);
            TodayNotTipUtil.toDayNotTip(TodayNoTipsConst.ComposeJewelOneKeyTip, this.doOneKeyComposeImpl, loc9, null, Alert.OK | Alert.CANCEL);
            return;
        }

        /* internal  */doOneKeyComposeImpl(): void
        {
            var loc2=0;
            var loc4=null;
            var loc5=0;
            var loc6=0;
            var loc7=null;
            var loc1=new Map<any, any> /* flash.utils.Dictionary */();
            var loc3=Cache.instance.pack.backPackCache.getTypeItems(ECategory._ECategoryProp, EProp._EPropJewelComposeRune);
            var loc8=0;
            var loc9=loc3;
            for(loc4 of loc9) 
            {
                if (this._useBindMaterial.selected) 
                {
                    loc1[loc4.uid] = loc4.itemAmount;
                }
                else if (!ItemsUtil.isBind(loc4)) 
                {
                    loc1[loc4.uid] = loc4.itemAmount;
                }
                ++loc2;
            }
            loc6 = this._jewelDp.length;
            loc5 = 0;
            while (loc5 < loc6) 
            {
                loc7 = this._jewelDp.getItemAt(loc5) as ItemData;
                loc1[loc7.uid] = loc7.itemAmount;
                ++loc2;
                if (loc2 >= 90) 
                {
                    break;
                }
                ++loc5;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.ComposeJewelOneKey_DoCompose, {"autoBuy": this._autoBuy.selected, "materials": loc1}));
            return;
        }

        /* internal  */getPropData(arg1: int): MaterialItemData
        {
            var loc1=0;
            var loc3=null;
            var loc2=this._materialDp.length;
            loc1 = 0;
            while (loc1 < loc2) 
            {
                if ((loc3 = this._materialDp.getItemAt(loc1) as MaterialItemData).itemData.effect <= arg1 && loc3.itemData.effectEx >= arg1) 
                {
                    return loc3;
                }
                ++loc1;
            }
            return null;
        }

        public updateJewelDatas(): void
        {
            var loc1=null;
            var loc2=null;
            var loc4=null;
            var loc5=0;
            this._jewelTypeDic = new Map<any, any> /* flash.utils.Dictionary */();
            this._jewelDp.removeAll();
            var loc3=Cache.instance.pack.stonePackCache.getJewelItems();
            var loc6=0;
            var loc7=loc3;
            for(loc4 of loc7) 
            {
                if (!(!JewelUtil.isNeedSpecialCompose(loc4) && JewelUtil.isCanCompose(loc4))) 
                {
                    continue;
                }
                if (!(JewelUtil.isNormalJewel(loc4) && this._normalSelecter.checkJewel(loc4) || !JewelUtil.isNormalJewel(loc4) && this._specialSelecter.checkJewel(loc4))) 
                {
                    continue;
                }
                this._jewelDp.addItem(loc4);
                loc1 = loc4.type + "_" + loc4.itemInfo.item.level + "_" + loc4.itemInfo.item.itemLevel;
                if (this._jewelTypeDic[loc1] == null) 
                {
                    loc2 = new JewelTypeDicData();
                    loc2.type = loc4.type;
                    loc2.level = loc4.itemInfo.item.level;
                    loc2.itemLevel = loc4.itemInfo.item.itemLevel;
                    loc2.amount = loc4.itemAmount;
                    this._jewelTypeDic[loc1] = loc2;
                    continue;
                }
                (this._jewelTypeDic[loc1] as JewelTypeDicData).amount = (this._jewelTypeDic[loc1] as JewelTypeDicData).amount + loc4.itemAmount;
            }
            this._cost = 0;
            loc5 = EquipAdvanceConfig.instance.getJewelComposeCost();
            loc6 = 0;
            loc7 = this._jewelTypeDic;
            for(loc2 of loc7) 
            {
                this._cost = this._cost + loc5 * Math.floor(loc2.amount / 4);
            }
            this._jewelList.dataProvider = this._jewelDp;
            this._costLabel.text = String(this._cost);
            return;
        }

        /* internal */ /* var */_materialDp: DataProvider;

        /* internal */ /* var */_materialList: GTileList;

        /* internal */ /* var */_changeModeBtn: ComposeJewelChangeBtn;

        /* internal */ /* var */_jewelDp: DataProvider;

        /* internal */ /* var */_jewelList: GTileList;

        /* internal */ /* var */_normalSelecter: ComposeJewelNormalSelecter;

        /* internal */ /* var */_specialSelecter: ComposeJewelSpecialSelecter;

        /* internal */ /* var */_optBtn: GButton;

        /* internal */ /* var */_cost: int;

        /* internal */ /* var */_costLabel: GLabel;

        /* internal */ /* var */_useBindMaterial: GCheckBox;

        /* internal */ /* var */_autoBuy: GCheckBox;

        /* internal */ /* var */_buyDirect: GCheckBox;

        /* internal */ /* var */_descText: GTextFiled;

        /* internal */ /* var */_jewelTypeDic: Map<any, any> /* flash.utils.Dictionary */;
    }
}


class JewelTypeDicData
{
    public JewelTypeDicData()
    {
        super();
        return;
    }

    public /* var */type: int;

    public /* var */level: int;

    public /* var */itemLevel: int;

    public /* var */amount: int;
}

class PropTypeDicData extends Object
{
    public PropTypeDicData()
    {
        super();
        return;
    }

    public /* var */prop: ItemData;

    public /* var */amount: int;

    public /* var */hasUnBind: Boolean;
